package com.foxit.pdf2html5.web.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.foxit.pdf2html5.common.TaskStore;
import com.foxit.pdf2html5.common.utils.Base64;
import com.foxit.pdf2html5.common.utils.GetBean;
import com.foxit.pdf2html5.common.utils.TFSFileUntil;
import com.foxit.pdf2html5.common.utils.ZipFileUtil;
import com.foxit.pdf2html5.tbschedule.task.ConversionHistoryInfo;
import com.foxit.pdf2html5.web.common.Config;
import com.google.code.morphia.Datastore;
import com.google.code.morphia.mapping.Mapper;
@Controller
@Scope("prototype")
public class EditorRootController {
	
	private static final Logger log = Logger.getLogger(EditorRootController.class);
    protected HttpServletRequest httpRequest;
    protected HttpServletResponse httpResponse;
    protected String fileId = null;
    protected String docShareId = null;
    protected String docInfoId = null;
    protected String etag = "";
    private String compressType = ".zip";
    private String tempFilePath = "";
    
	private boolean GetDocData() throws IOException{
		Datastore datastore = GetBean.getDatastore();
		ConversionHistoryInfo taskInfo = datastore.createQuery(ConversionHistoryInfo.class).field(Mapper.ID_KEY).equal(Base64.encode(this.fileId)).get();
		if(taskInfo != null){
			int status = taskInfo.getConversionStatus();
			if(status == 4){
				InputStream inStream = TFSFileUntil.readerContent(Config.FOXIT_EDITOR_CACHE_URL_KEY + this.fileId + ".zip");
				if(inStream != null){
					FileUtils.copyInputStreamToFile(inStream, new File(Config.TEMP_FILE_PATH_KEY  + File.separator + this.fileId + ".zip"));
					File tempFile = new File(Config.TEMP_FILE_PATH_KEY  + File.separator + this.fileId + ".zip");
					if(tempFile.exists()){
						File destFile = new File(tempFilePath);
						if(!destFile.exists())destFile.mkdirs();
						ZipFileUtil.unZipFiles(tempFile, tempFilePath  + File.separator + this.fileId);
					}
					return true;
				}
				
			}
		}
		
    	
    	return false;
	}
	@RequestMapping("/LoadReader.do")
	public String ShowWebReaderRootPage(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) throws IOException {
		this.httpRequest = request;
		this.httpResponse = response;
		this.fileId = this.httpRequest.getParameter("fileid");
		
		if(fileId == null || fileId.isEmpty()){
            return "";
		}
		tempFilePath = this.httpRequest.getSession().getServletContext().getRealPath("temp");
		System.out.println(tempFilePath  + File.separator + this.fileId);
		File binFile = new File(tempFilePath  + File.separator + this.fileId);
		if(!binFile.exists()){
			GetDocData();
		}
		if(!binFile.exists())return "";
		map.addAttribute("fileid", this.fileId);
		map.addAttribute("fname", this.fileId);
		map.addAttribute("qrcode", "/temp/" + this.fileId + "/docBin/");
		map.addAttribute("dtype",1);
		return "/demo.ftl";
			
	}
}
